﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace TextMining.Models
{
    public class CategoryList
    {
        public Dictionary<string, List<string>> CateList;

        public CategoryList()
        {
            CateList = new Dictionary<string,List<string>>();
        }
        public void InitialCateList()
        {
            string sFolder = HttpContext.Current.Server.MapPath("\\Category\\");
            DirectoryInfo di = new DirectoryInfo(sFolder);
            FileInfo[] aFileInfos = di.GetFiles("*.txt");

            int iNumOfCate = aFileInfos.Length;

            foreach (var item in aFileInfos)
            {
                try
                {
                    Dictionary<string, int> dicCategory = new Dictionary<string, int>();
                    List<string> listWord = new List<string>();
                    using (StreamReader sr = new StreamReader(item.FullName))
                    {
                        string sContent = sr.ReadToEnd();
                        if (!string.IsNullOrEmpty(sContent) && sContent != "")
                        {
                            string[] arrItem = sContent.Split(new Char[] { ' ', ',', '.', ':', '\t' });

                            foreach (var word in arrItem)
                            {

                                if (!string.IsNullOrEmpty(word))
                                {

                                    if (listWord.Contains(word) == false)
                                    {
                                        listWord.Add(word);
                                    }
                                }
                            }

                        }
                        CateList.Add(item.Name.Substring(0,item.Name.IndexOf(".")),listWord);
                        
                    }

                }
                catch (Exception ex)
                {

                }
            }
        }
    }
}